package com.kzx.ticket.utils;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author kongz
 * @version 1.0 2021/11/16 15:33
 * @description http访问
 */
public class HttpUtils {
    /**
     * HTTP POST请求
     * @param param 请求参数
     * @return 请求信息
     */
    public static Map<String, Object> httpPost(Map<String, Object> param) {

        CloseableHttpClient httpClient = null;
        CloseableHttpResponse httpResponse = null;
        Map<String, Object> result = new HashMap<>();
        try {
            String url = param.get("url").toString();
            httpClient = HttpClientBuilder.create().build();
            //JSONObject jsonObj = (JSONObject) param.get("param");
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(2000000).setSocketTimeout(2000000).build();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(requestConfig);
            httpPost.setHeader("Content-Type", "multipart/form-data; boundary=<calculated when request is sent>");
            httpPost.setHeader("Accept", "*/*");
            httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
            httpPost.setHeader("Connection", "keep-alive");

            //StringEntity entity = new StringEntity(jsonObj.toString(), StandardCharsets.UTF_8);//解决中文乱码问题
            //      entity.setContentEncoding("UTF-8");
            //if (!param.containsKey("unEncoding")) {
            //    entity.setContentEncoding("UTF-8");
            //}
            //entity.setContentType("application/json");
            //httpPost.setEntity(entity);
            //请求
            httpResponse = httpClient.execute(httpPost);
            HttpEntity responseEntity = httpResponse.getEntity();
            String resultStr = EntityUtils.toString(responseEntity, "utf-8");
            //正常返回
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                result.put("status", true);
                result.put("message", resultStr);
            } else {
                result.put("status", false);
                result.put("message", httpResponse.getStatusLine().getReasonPhrase());
            }
        } catch (Exception e) {
            result.put("status", false);
            result.put("message", e.getMessage());
        } finally {
            closeHttpClient(httpClient);
            closeHttpResponse(httpResponse);
        }
        return result;
    }

    /**
     * 关闭httpClient连接
     */
    public static void closeHttpClient(CloseableHttpClient httpClient) {
        if (httpClient != null) {
            try {
                httpClient.close();
            } catch (IOException e) {
            }
        }
    }

    /**
     * 关闭httpResponse连接
     */
    private static void closeHttpResponse(CloseableHttpResponse httpResponse) {
        if (httpResponse != null) {
            try {
                httpResponse.close();
            } catch (IOException e) {
            }
        }
    }
}
